<template>
  <div>
    <a-button class="editable-add-btn" @click="handleAdd">
      Add
    </a-button>
    <span>价格信息</span>
    <a-table bordered :data-source="dataSource" :columns="columns">

      <template slot="name" slot-scope="text, record">
        <editable-cell :text="text" @change="onCellChange(record.key, 'name', $event)" />
      </template>

      <!-- 规格条码 -->
      <template slot="SpecificationCode" slot-scope="text,record">
        <a-input :text="text" v-model="record.SpecificationCode" placeholder="请输入规格条码" size="large" style="width:150px" />
        {{record.SpecificationCode}}
      </template>
      <!-- 采购价 -->
      <template slot="purchasePrice" slot-scope="text,record">
        <a-input-number v-model="record.purchasePrice" size="large" style="width:80px" />
      </template>
      <!-- 分销价钱 -->
      <template slot="distributionPrice" slot-scope="text,record">
        <a-input-number :text="text" v-model="record.distributionPrice" size="large" style="width:80px" />
      </template>
      <!-- 市场价钱 -->
      <template slot="marketPrice" slot-scope="text,record">
        <a-input-number :text="text" v-model="record.marketPrice" size="large" style="width:80px" />
      </template>
      <!-- 折扣率 -->
      <template slot="discountRate" slot-scope="text,record">
        <a-input-number :text="text" v-model="record.discountRate" size="large" style="width:80px" />
      </template>
      <!-- 零售价 -->
      <template slot="slaePrice" slot-scope="text,record">
        <a-input-number :text="text" v-model="record.slaePrice" size="large" style="width:80px" />
      </template>
    </a-table>
  </div>
</template>
<script>
const EditableCell = {
  template: `
      <div class="editable-cell">
        <div v-if="editable" class="editable-cell-input-wrapper">
          <a-input :value="value" @change="handleChange" @pressEnter="check" /><a-icon
            type="check"
            class="editable-cell-icon-check"
            @click="check"
          />
        </div>
        <div v-else class="editable-cell-text-wrapper">
          {{ value || ' ' }}
          <a-icon type="edit" class="editable-cell-icon" @click="edit" />
        </div>
      </div>
    `,
  props: {
    text: String,
  },
  data() {
    return {
      value: this.text,
      editable: false,
    };
  },
  methods: {
    handleChange(e) {
      const value = e.target.value;
      this.value = value;
    },
    check() {
      this.editable = false;
      this.$emit('change', this.value);
    },
    edit() {
      this.editable = true;
    },
  },
};

export default {
  components: {
    EditableCell,
  },
  data() {
    return {
      dataSource: [
        {
          key: '0',
          name: '1.11',
          SpecificationCode: '',//规格条码
          purchasePrice: '20',// 采购价
          distributionPrice: '60',//分销价
          marketPrice: '70',//市场价
          discountRate: '',//折扣率
          slaePrice: '80',//销售价
        },
        {
          key: '1',
          name: '1.222',
          SpecificationCode: '',//规格条码
          purchasePrice: '20',// 采购价
          distributionPrice: '60',//分销价
          marketPrice: '70',//市场价
          discountRate: '',//折扣率
          slaePrice: '80',//销售价
        },
      ],
      count: 20,
      columns: [
        {
          title: '分量',
          dataIndex: 'name',
        },
        {
          title: '规格条形码',
          dataIndex: 'SpecificationCode',
          scopedSlots: { customRender: 'SpecificationCode' },
          align: "center"
        },
        {
          title: '采购价',
          dataIndex: 'purchasePrice',
          scopedSlots: { customRender: 'purchasePrice' },
          align: "center"

        },
        {
          title: '分销价',
          dataIndex: 'distributionPrice',
          scopedSlots: { customRender: 'distributionPrice' },
          align: "center"

        },
        {
          title: '市场价',
          dataIndex: 'marketPrice',
          scopedSlots: { customRender: 'marketPrice' },
        },
        {
          title: '折扣率',
          dataIndex: 'discountRate',
          scopedSlots: { customRender: 'discountRate' },
        },
        {
          title: '零销价',
          dataIndex: 'slaePrice',
          scopedSlots: { customRender: 'slaePrice' },
        },
      ],
    };
  },
  methods: {
    onCellChange(key, dataIndex, value) {
      const dataSource = [...this.dataSource];
      const target = dataSource.find(item => item.key === key);
      if (target) {
        target[dataIndex] = value;
        this.dataSource = dataSource;
      }
    },
    onDelete(key) {
      const dataSource = [...this.dataSource];
      this.dataSource = dataSource.filter(item => item.key !== key);
    },
    handleAdd() {
      const { count, dataSource } = this;
      const newData = {
        key: count,
        name: `Edward King ${count}`,
        age: 32,
        address: `London, Park Lane no. ${count}`,
      };
      this.dataSource = [...dataSource, newData];
      this.count = count + 1;
    },
  },
};
</script>
<style>
.editable-cell {
  position: relative;
}

.editable-cell-input-wrapper,
.editable-cell-text-wrapper {
  padding-right: 24px;
}

.editable-cell-text-wrapper {
  padding: 5px 24px 5px 5px;
}

.editable-cell-icon,
.editable-cell-icon-check {
  position: absolute;
  right: 0;
  width: 20px;
  cursor: pointer;
}

.editable-cell-icon {
  line-height: 18px;
  display: none;
}

.editable-cell-icon-check {
  line-height: 28px;
}

.editable-cell:hover .editable-cell-icon {
  display: inline-block;
}

.editable-cell-icon:hover,
.editable-cell-icon-check:hover {
  color: #108ee9;
}

.editable-add-btn {
  margin-bottom: 8px;
}
</style>
